/*
 * Copyright (C) 2016 huanghaibin_dev <huanghaibin_dev@163.com>
 * WebSite https://github.com/MiracleTimes-Dev
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.elvis.library;


import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.sysappcomponents.contact.entity.Holder;

import java.util.ArrayList;
import java.util.List;

/**
 * 基本的适配器
 */
abstract class BaseRecyclerAdapter<T> extends BaseItemProvider {

    private List<T> mItems;
    Context mContext;

    BaseRecyclerAdapter(Context context) {
        mContext = context;
        this.mItems = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public T getItem(int position) {
        if (position < 0 || position >= mItems.size())
            return null;
        return mItems.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder;
        if (component == null) {
            holder = onCreateDefaultViewHolder(componentContainer);
            cpt = holder.getView();
            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);
        } else {
            cpt = component;
            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (ViewHolder) cpt.getTag();
        }
        onBindViewHolder(holder, getItem(position), position);
        return cpt;
    }

    abstract ViewHolder onCreateDefaultViewHolder(ComponentContainer parent);

    abstract void onBindViewHolder(ViewHolder holder, T item, int position);


    @SuppressWarnings("unused")
    void addAll(List<T> items) {
        if (items != null && items.size() > 0) {
            mItems.addAll(items);
            notifyDataSetItemRangeInserted(mItems.size(), items.size());
        }
    }

    final void addItem(T item) {
        if (item != null) {
            this.mItems.add(item);
            notifyDataSetItemChanged(mItems.size());
        }
    }

    @SuppressWarnings("unused")
    final List<T> getItems() {
        return mItems;
    }

    static class ViewHolder {
        Component view;

        public ViewHolder(Component view) {
            this.view = view;
        }

        public Component getView() {
            return view;
        }
    }
}
